package com.sf.rbac.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sf.rbac.cons.RedisConst;
import com.sf.rbac.domain.Employee;
import com.sf.rbac.ex.BusinessException;
import com.sf.rbac.mapper.EmployeeMapper;
import com.sf.rbac.mapper.PermissionMapper;
import com.sf.rbac.service.ILoginService;
import com.sf.rbac.util.VerifyCodeUtil;
import com.sf.rbac.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public Map<String, String> code() {
        // 1调用工具生成验证码
        Map<String, String> map = VerifyCodeUtil.generateVerifyCode();
        //2 map :code(redis 的 value 验证码)  ,uuid(作为redis key 的唯一标识) ,img
        String code = map.get("code");
        String uuid = map.get("uuid");
        System.out.println(code);
        //3 把数据放到redis中 String key:verify_code: +uuid
        // 在这里把数据放到redis 中后续还需要取出来
        redisTemplate.opsForValue().set(RedisConst.VERIFY_CODE_PREFIX+uuid,code,RedisConst.VERIFY_CODE_OVERTIME, TimeUnit.SECONDS);
        //4 把code 移除
        map.remove("code");
        return map;
    }

    @Override
    public Employee login(LoginVo loginVo) {
        //1 账号不能为空
        if(loginVo.getUsername() ==null){
            throw new BusinessException("账号不能为空");
        }
        //2 密码不能为空
        if(loginVo.getPassword()==null){
            throw new BusinessException("密码不能为空");
        }
        // 3 验证码不能为空
        if(loginVo.getCode()==null){
            throw new BusinessException("验证码不能为空");
        }
        // 4 uuid 不能为空
        if(loginVo.getUuid()==null){
            throw new BusinessException("非法操作");
        }
        // 5 验证码校验
        String code = redisTemplate.opsForValue().get(RedisConst.VERIFY_CODE_PREFIX + loginVo.getUuid());
        if(!loginVo.getCode().equals(code)){
            throw new BusinessException("验证码错误");
        }
        // 6 根据账号和密码获取数据库数据
       Employee employee= employeeMapper.login(loginVo.getUsername(),loginVo.getPassword());
        //7 判断employee 是否为空
        if(employee==null){
            throw new BusinessException("账号密码错误");
        }
        //9把当前登录的用户信息保存到redis中
        // key :string  values :employee->json
        redisTemplate.opsForValue().set(RedisConst.EMPLOYEE_INFO_PREFIX+employee.getId(), JSONObject.toJSONString(employee),
                RedisConst.EMPLOYEE_INFO_EXPIRE_TIME,TimeUnit.SECONDS);
        //10 把用户所具备的权限表达式集合放到redis中
      Set<String> set=permissionMapper.selectExpressionsByEmpId(employee.getId());
      redisTemplate.opsForValue().set(RedisConst.EMPLOYEE_EXPRESSION_PREFIX+employee.getId(),JSONObject.toJSONString(set),
              RedisConst.EMPLOYEE_EXPRESSION_EXPIRE_TIME,TimeUnit.SECONDS);
        return employee;
    }

    @Override
    public void logout(String userId) {
        //删除redis 中的用户信息
        redisTemplate.delete(RedisConst.EMPLOYEE_INFO_PREFIX+userId);
        //删除redis 中的用户所具有的权限集合信息
        redisTemplate.delete(RedisConst.EMPLOYEE_EXPRESSION_PREFIX+userId);
    }
}
